class RecruitscrapyDownloaderMiddleware:
 
    def process_request(self, request, spider):
        # 获取request内的url
        request_url = request.url
       # 登录请求
        if "login" in request.url:
            # 定义UA
            user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
            # 去除非必要的日志输出
            options = webdriver.ChromeOptions()
            options.add_experimental_option("excludeSwitches", ["enable-logging"])
 
            options.add_argument('--user-agent={}'.format(str(user_agent)))
            webBrowser = Chrome(options=options)
            # 发起Chrome浏览器访问request内的url
            webBrowser.get(request_url)
            # 用于输入验证码，提交
            time.sleep(10)
            # 存储cookie
            spider.cookies = webBrowser.get_cookies()
        else:
            # 数据类请求
            req = requests.session()  # 会话
            for cookie in spider.cookies:
                req.cookies.set(cookie['name'], cookie["value"])
            req.headers.clear()  # 清空头
            new_page = req.get(request_url)
 
            # 创建响应对象,返回
            res = HtmlResponse(url=request_url, body=new_page.text,
                               encoding='utf-8', request=request)
            return res
